package com.dpfmanage.api.project.dpfmanage.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class CopyUtil {
    public static <T> T copyObject(Object source, Class<T> c) {
        try {
            T t = c.newInstance();
            BeanUtils.copyProperties(source, t);
            return t;
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> copylist(List source, Class<T> c) {
        try {
            List<T> ts = new ArrayList<>();
            for (int i = 0; i < source.size(); i++) {
                T t = c.newInstance();
                BeanUtils.copyProperties(source.get(i), t);
                ts.add(t);
            }
            return ts;
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
